package work.MrSun.xyq.controller;
/**
 * @Author MrSun
 * @Date 2023/4/13 19:51
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import work.MrSun.xyq.model.XyqAttend;
import work.MrSun.xyq.model.XyqMessage;
import work.MrSun.xyq.service.XyqAttendService;
import work.MrSun.xyq.service.XyqMessageDetailService;
import work.MrSun.xyq.service.XyqMessageImagesService;
import work.MrSun.xyq.service.XyqUserService;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 我的参与控制层----只要是点赞  收藏 转发都会显示
 */
@RestController
public class AttendController {
    @Autowired
    private XyqUserService xyqUserService;
    //详细信息
    @Autowired
    private XyqMessageDetailService xyqMessageDetailService;
    @Autowired
    private XyqMessageImagesService xyqMessageImagesService;
    @Autowired
    private XyqAttendService xyqAttendService;

    /**
     * 获取我的全部参与信息
     *
     * @param userId
     * @param pageNumber
     * @return
     */
    @PostMapping("/getMessage/getAttendMessageByUserId/{userId}/{pageNumber}")
    public List<XyqMessage> getAttendMessageByUserId(@PathVariable Integer userId, @PathVariable Integer pageNumber) {
        //开始分页
        PageHelper.startPage(pageNumber, 5);
        List<XyqAttend> allAttendMessageByUserId = xyqAttendService.getAllAttendMessageByUserId(userId);
        //进行封装
        PageInfo<XyqAttend> pageInfo = new PageInfo<>(allAttendMessageByUserId);
        if (pageInfo.getPageNum() < pageNumber) {
            List list1 = new LinkedList();
            list1.add(200);
            return list1;
        }
        //从pageInfo中获取当前页的消息列表list
        List<XyqAttend> list = pageInfo.getList();
        //存放在List集合中
        ArrayList<XyqMessage> list1 = new ArrayList<>();
        //并遍历列表中的所有消息。通过每条消息的ID
        for (int i = 0; i < list.size(); i++) {
            Integer messageId = list.get(i).getMessageId();
            XyqMessage message = xyqMessageDetailService.getById(messageId);
            list1.add(message);
        }
        //将消息列表、用户服务对象和消息图片服务对象作为参数传入，并返回处理后的消息列表。
        return new MessageDetailController().getImage(list1, xyqUserService, xyqMessageImagesService);
    }
}
